/**
 * 数据库连接
 */
const mongoose = require('mongoose');
const config = require('../config');
const logger = require('../utils/logger');
const util = require('../utils/util');

mongoose.connect(config.mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false });

const db = mongoose.connection;

db.on('error', (err) => {
  logger.error('数据库连接失败:' + err);
});

db.on('open', () => {
  logger.info('数据库连接成功:' + config.mongoUrl);
  // 初始化数据库，保证测试代码一定存在
  // util.resetDb();
});
